package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.AlbumInfoService;
import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;


	@Autowired
	private VodService vodService;

	/**
	 * 音视频文件上传点播平台
	 *
	 * @param file 文件
	 * @return {mediaFileId:"文件唯一标识",mediaUrl:"在线播放地址"}
	 */
	@Operation(summary = "音视频文件上传点播平台")
	@PostMapping("/trackInfo/uploadTrack")
	public Result<Map<String, String>> uploadTrack(@RequestParam("file") MultipartFile file) {
		Map<String, String> map = vodService.uploadTrack(file);
		return Result.ok(map);
	}

	/**
	 * 该接口必须登录才能访问
	 * 保存声音
	 *
	 * @param trackInfo
	 * @return
	 */
	@GuiGuLogin(required = true)
	@Operation(summary = "保存声音")
	@PostMapping("/trackInfo/saveTrackInfo")
	public Result saveTrackInfo(@RequestBody TrackInfo trackInfo) {
		//1.获取当前登录用户ID
		Long userId = AuthContextHolder.getUserId();
		//2.调用业务层保存声音-发起审核任务
		trackInfoService.saveTrackInfo(userId, trackInfo);
		return Result.ok();
	}
	/**
	 * 条件分页查询当前用户声音列表（包含声音统计信息）
	 *
	 * @param page           页码
	 * @param limit          页大小
	 * @param trackInfoQuery 查询条件
	 * @return
	 */
	@GuiGuLogin(required = true)
	@Operation(summary = "条件分页查询当前用户声音列表（包含声音统计信息）")
	@PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
	public Result<Page<TrackListVo>> getUserTrackPage(
			@PathVariable int page,
			@PathVariable int limit,
			@RequestBody TrackInfoQuery trackInfoQuery
	) {
		//1.获取当前用户ID
		Long userId = AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		//2.构建分页所需分页对象
		Page<TrackListVo> pageInfo = new Page<>(page, limit);
		//3.查询业务层(持久层)获取分页数据
		pageInfo = trackInfoService.getUserTrackPage(pageInfo, trackInfoQuery);
		return Result.ok(pageInfo);
	}

	/**
	 * 根据声音ID查询声音信息
	 *
	 * @param id
	 * @return
	 */
	@Operation(summary = "根据声音ID查询声音信息")
	@GetMapping("/trackInfo/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id) {
		TrackInfo trackInfo = trackInfoService.getById(id);
		return Result.ok(trackInfo);
	}

	/**
	 * 修改声音信息
	 * @param id
	 * @param trackInfo
	 * @return
	 */
	@Operation(summary = "修改声音信息")
	@PutMapping("/trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id, @RequestBody TrackInfo trackInfo){
		trackInfoService.updateTrackInfo(trackInfo);
		return Result.ok();
	}

	/**
	 * 删除声音记录
	 * @param id
	 * @return
	 */
	@Operation(summary = "删除声音记录")
	@DeleteMapping("/trackInfo/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	/**
	 * 该方法不登录可以访问，但用户登录状态就可以从ThreadLocal获取用户ID
	 * 分页查询当前用户可见声音列表-动态判断声音付费标识
	 * @param albumId 专辑ID
	 * @param page 页码
	 * @param limit 页大小
	 * @return
	 */
	@GuiGuLogin(required = false)
	@Operation(summary = "分页查询当前用户可见声音列表-动态判断声音付费标识")
	@GetMapping("/trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	public Result<Page<AlbumTrackListVo>> getAlbumTrackPage(
			@PathVariable Long albumId,
			@PathVariable int page,
			@PathVariable int limit
	){
		//1.获取当前登录用户信息
		Long userId = AuthContextHolder.getUserId();
		//2.构建分页所需Page对象
		Page<AlbumTrackListVo> pageInfo = new Page<>(page, limit);
		//3.调用业务层获取业务数据
		pageInfo = trackInfoService.getAlbumTrackPage(pageInfo, albumId, userId);
		return Result.ok(pageInfo);
	}

	/**
	 * 查询声音统计信息
	 * @param trackId
	 * @return
	 */
	@Operation(summary = "查询声音统计信息")
	@GetMapping("/trackInfo/getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId){
		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}

	/**
	 * 基于当前选择购买起始声音得到分集购买列表
	 *
	 * @param trackId
	 * @return [{name:"本集",price:1,trackCount:1},{name:"后10集",price:10,trackCount:10}...{name:"全集/45集",price:45,trackCount:45}]
	 */
	@GuiGuLogin
	@Operation(summary = "基于当前选择购买起始声音得到分集购买列表")
	@GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
	public Result<List<Map<String, Object>>> getUserTrackPaidList(@PathVariable Long trackId) {
		//1.从ThreadLocal中获取当前登录用户ID
		Long userId = AuthContextHolder.getUserId();
		//2.调用业务逻辑获取分集购买列表
		List<Map<String, Object>> list = trackInfoService.getUserTrackPaidList(userId, trackId);
		return Result.ok(list);
	}
	/**
	 * 查询当前用户未购买声音列表
	 *
	 * @param trackId    点击付费标识声音ID，将该声音作为起始标准
	 * @param trackCount 购买数量
	 * @return 待购声音列表
	 */
	@GuiGuLogin
	@Operation(summary = "查询当前用户未购买声音列表")
	@GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
	public Result<List<TrackInfo>> getWaitBuyTrackList(@PathVariable Long trackId, @PathVariable Integer trackCount) {
		//1.从ThreadLocal中获取当前登录用户ID
		Long userId = AuthContextHolder.getUserId();
		//2.调用业务逻辑获取待购声音列表
		List<TrackInfo> list = trackInfoService.getWaitBuyTrackList(userId, trackId, trackCount);
		return Result.ok(list);
	}

}

